import AntModal from "@/components/pt/dialog/AntModal";
import {
  getBizCustomer,
  addBizCustomer,
  updateBizCustomer,
  findMaxSort,
} from "@/api/biz/bizCustomer";
import {
  addBizCustomerShadowInfo,
  listBizCustomerShadowInfo,
} from "@/api/biz/bizCustomerShadowInfo";
import storage from "store";
import { ACCESS_TOKEN } from "@/store/mutation-types";
export default {
  name: "CreateForm",
  props: {
    statusOptions: {
      type: Array,
      required: true,
    },
    typeOptions: {
      type: Array,
      required: true,
    },
    progressOptions: {
      type: Array,
    },
    levelOptions: { type: Array },
  },
  components: {
    AntModal,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "添加左心房客户推送信息",
      // 表单参数
      form: {},
      rules: {},
      customerShadowList: [],
      isinputshow: false,
      sform: {
        level: undefined,
        ideasType: undefined,
        nextTime: undefined,
        mark: undefined,
        takes: undefined,
        progress: undefined,
      },
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        uploadStatus: "",
        fileList: [],
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + storage.get(ACCESS_TOKEN) },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API,
      },
    };
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,

        companyId: undefined,

        carModel: undefined,

        cityName: undefined,

        cusName: undefined,

        houseType: undefined,

        phone: undefined,

        price: undefined,

        type: undefined,

        shareDate: undefined,

        accumulation: undefined,

        carStatus: undefined,

        policy: undefined,

        sex: undefined,

        social: undefined,

        dailTime: undefined,

        isDail: undefined,

        clueId: undefined,

        ip: undefined,

        sort: undefined,

        status: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "客户信息导入";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizCustomerId = row.id;
      getBizCustomer(bizCustomerId).then((response) => {
        this.form = response.data;
        this.formTitle = "修改客户信息";
        this.spinning = !this.spinning;
      });
    },
    handleShadow(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizCustomerInfoId = row.id;
      getBizCustomer(bizCustomerInfoId).then((response) => {
        this.form = response.data;
        this.spinning = !this.spinning;
      });
      this.getcustomershadow(row.id);
    }, // 获取跟踪信息
    getcustomershadow(customerId) {
      const query = { customerId: customerId, pageSize: 5, pageNo: 1 };
      listBizCustomerShadowInfo(query).then((res) => {
        this.customerShadowList = res.data.list;
      });
    },
    handleChange(info) {
      const status = info.file.status;
      if (status !== "uploading") {
        console.log(info.file, info.fileList);
      }
      if (status === "done") {
        this.$message.success(`${info.file.response.msg}`);
      } else if (status === "error") {
        this.$message.error(`${info.file.name} 文件导入失败！`);
      }
    },
    // 提交跟踪信息
    shadowSubimt() {
      var form = {
        level: this.sform.level,
        name: this.form.name,
        phone: this.form.phone,
        nextTime: this.sform.nextTime,
        // mark: this.sform.mark,
        takes: this.sform.takes,
        customerId: this.form.id,
        progress: this.sform.progress,
      };
      // console.log(this.fileList);
      // var urls = form.images
      //   .map(function (obj, index) {
      //     return obj.response.data.url;
      //   })
      //   .join(";");
      // form.images = urls;

      if (form.level && form.takes && form.nextTime) {
        addBizCustomerShadowInfo(form).then((res) => {
          this.$message.success("添加成功");
          this.$emit("ok");
          this.getcustomershadow(this.form.id);
          this.isinputshow = false;
          this.sform.level = undefined;
          this.sform.nextTime = undefined;
          this.sform.takes = undefined;
          this.sform.progress = undefined;
        });
      } else {
        this.$message.error("请完善跟进信息");
      }
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form));
          if (this.form.id !== undefined) {
            updateBizCustomer(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          } else {
            addBizCustomer(saveForm).then((response) => {
              this.$message.success("新增成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/customer/index";
      this.$router.push(index);
    },
    typeFormat(type) {
      if (type) {
        return this.selectDictLabel(this.typeOptions, type);
      } else {
        return "";
      }
    },
    levelFormat(level) {
      if (level) {
        return this.selectDictLabel(this.levelOptions, level);
      } else {
        return "";
      }
    },
    processFormat(process) {
      if (process) {
        return this.selectDictLabel(this.progressOptions, process);
      } else {
        return "";
      }
    },
  },
};
